import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LKQ
 * @date 2021/12/28 9:46
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int[] nums1 = {4, 9, 5}, nums2 = {9, 4,9,8,4};
        solution2.intersect(nums1,nums2);
    }

    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int m = nums1.length, n = nums2.length, p1 =0, p2 = 0, count= 0 ;
        int[] ans = new int[Math.min(m, n )];
        while (p1 < m && p2 < n) {
            if (nums1[p1] == nums2[p2]) {
                ans[count++] = nums1[p1];
                p1++;
                p2++;
            }else {
                if (nums1[p1] > nums2[p2]) {
                    p2++;
                }else {
                    p1++;
                }
            }
        }
        return Arrays.copyOfRange(ans, 0, count);
    }
}
